
//
//  GuideHomeViewController.m
//  BeautifulShoppings
//
//  Created by GivenBmli on 16/9/7.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "GuideHomeViewController.h"
#import "GuideHomeCollectionViewCell.h"
#import "NetworkHandler.h"
#import "UIImageView+WebCache.h"
#import "ToolDB.h"
#import "ToolModel.h"
#import "TAMacro.h"
#define URL @"http://jewel-cdn.idailywatch.com/app/jewel/list/cover_jewel/zh-hans?page=1&ver=iphone&app_ver=16"

@interface GuideHomeViewController ()
@property (nonatomic ,strong)UIWebView *webView;
@property (nonatomic ,strong)UIImageView * imageView;
@property (nonatomic ,strong)UIScrollView * scrollView;
@property (nonatomic ,strong)UIButton * like_Button;
@property (nonatomic ,strong)UIButton * collect_Button;

@end

@implementation GuideHomeViewController
- (void)viewDidAppear:(BOOL)animated {
    NSString *sql = @"SELECT title,cover_image_url,Id FROM t_activey;";
    NSMutableArray *array = [ToolDB selectFromSql:sql];
    BOOL saved = NO;
    for (ToolModel *model in array) {
        if ([model.title isEqualToString:self.STR]) {
            saved = YES;
            [self.like_Button setImage:[UIImage imageNamed:@"红心"] forState:UIControlStateNormal];
            break;
        }
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *butt = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [butt setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    butt.frame  = CGRectMake(0, 0, 50, 50);
    UIBarButtonItem *left= [[UIBarButtonItem alloc]initWithCustomView:butt];
    [butt addTarget:self action:@selector(clickLeft:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem =left;
    [self foundWebViwe];
}
- (void)clickLike:(UIButton*)send {
    NSString *sql = @"SELECT title,cover_image_url,Id FROM t_activey;";
    NSMutableArray *array = [ToolDB selectFromSql:sql];
    BOOL saved = NO;
    for (ToolModel *model in array) {
        if ([model.title isEqualToString:self.STR]) {
            saved = YES;
            break;
        }
    }
    if (saved == NO) {
        [ToolDB createTable];
        [self.like_Button setImage:[UIImage imageNamed:@"红心"] forState:UIControlStateNormal];
        NSString *sql2 = [NSString stringWithFormat:@"INSERT INTO t_activey(title,cover_image_url,ID) VALUES('%@','%@',%@);", self.STR,self.IMAGE,self.str];
        [ToolDB execSqlString:sql2];
    }else{
        [self.like_Button setImage:[UIImage imageNamed:@"心"] forState:UIControlStateNormal];
        NSString *sql2 = [NSString stringWithFormat:@"DELETE FROM t_activey where title = '%@' and cover_image_url = '%@';", self.STR,self.IMAGE];
        [ToolDB dele:sql2];
    }
}
- (void)foundWebViwe {
    self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
    [NetworkHandler getDataByURLString:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/posts/%@?show_baichuan=0",self.str] BodyString:nil WithDataBlock:^(id result) {
        NSDictionary *dic = [result objectForKey:@"data"];
        self.image = [dic objectForKey:@"cover_image_url"];
        self.str2 = [dic objectForKey:@"content_html"];
        [self.webView  loadHTMLString:self.str2 baseURL:[NSURL fileURLWithPath: [[NSBundle mainBundle]  bundlePath]]];
        [self.view addSubview:self.webView];
        [self foundImageView];
        
        self.like_Button = [UIButton buttonWithType:UIButtonTypeCustom];
        self.like_Button.frame =CGRectMake(0, SCREEN_HEIGHT - SCREEN_HEIGHT / 10 - 4, SCREEN_WIDTH, SCREEN_HEIGHT / 10 + 4);
        [self.like_Button setTitle:@"喜欢" forState:UIControlStateNormal];
        [self.like_Button setImage:[UIImage imageNamed:@"心"] forState:UIControlStateNormal];
        [self.like_Button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        self.like_Button.backgroundColor = [UIColor whiteColor];
        [self.like_Button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.like_Button addTarget:self action:@selector(clickLike:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:self.like_Button];
    }];
}
- (void)foundImageView{
    self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, -SCREEN_HEIGHT / 3, SCREEN_WIDTH, SCREEN_HEIGHT / 3)];
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:self.image] placeholderImage:[UIImage imageNamed:@"站位"]];
    self.webView.scrollView.contentInset = UIEdgeInsetsMake(SCREEN_HEIGHT / 3, 0, 0, 0 );
    [self.webView.scrollView addSubview:self.imageView];
}
- (void)clickLeft:(UIBarButtonItem*)qwe {
    [self.navigationController popViewControllerAnimated:YES];
}
@end
